﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
using System.ComponentModel.DataAnnotations;

namespace FCTC_Web.Models
{
    public class SessionUnit:EntityBase
    {
        List<SessionArrangement> UnitSession{get;set;}
        List<Team> _teams;
        public List<Team> GetTeams() { return _teams;  }
        public SessionUnit()
        {
            UnitSession = CH.GetAllData<SessionArrangement>().FindAll(i=>i.SessionUnit.ID == ID);
            var enrollments = CH.GetAllData<TeamEnrollment>();
            _teams = CH.GetAllData<Team>().FindAll(i => enrollments.FirstOrDefault(e => e.TeamID == i.ID) != null);
        }
        public virtual List<TeamEnrollment> TeamEnrollments { get; set; }

        [Display(Name = "小组名称"),MaxLength(50) ,Required(ErrorMessage = "请填写小组的名称")]
        public string UnitName { get; set; }

        [Display(Name = "小组类型"), MaxLength(50), Required(ErrorMessage = "请选择小组的类型")]
        public string UnitType { get; set; }

        [Display(Name = "小组状态"), MaxLength(50)]
        public string UnitStatus { get; set; }

        public string GetCompititionScore(int SAID, int leftTeamID, int rightTeamID)
        {
           var sa = UnitSession.FirstOrDefault(i=>i.ID == SAID && i.LeftTeamID == leftTeamID && i.RightTeamID == rightTeamID);
           return sa.GetScore();
        }

        public int GetTeamTotalScore(int teamId)
        {
            var sa = UnitSession.FindAll(i => i.SessionUnit.ID == ID && i.GetWinnerID() == teamId);
            return sa.Count*2;
        }

        public List<Team> SortByTotalScore()
        {
            return _teams.OrderByDescending(i => GetTeamTotalScore(i.ID)).ToList();
        }
    }
}